package com.yc.controller;

import com.yc.common.utils.UploadFileUtil;
import com.yc.service.impl.LeavereQuestsServiceImpl;
import com.yc.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author zxy
 * @version 1.0
 * @date 2023/7/10 9:12
 */
@Api(tags = "小程序投请假申请控制类")
//@RestController
public class LeaverequestController {

    @Resource
    private LeavereQuestsServiceImpl leavereQuestsService;

    /**
     * 数据存储请求（添加请假申请
     * @param stuid 学生id
     * @param reson 请假原因
     * @param starttime 开始时间
     * @param overtime 结束时间
     * @param newFilePath 文件上传后的地址
     * @return
     */
    @ApiOperation("数据存储请求（添加请假申请")
    @PostMapping("wx/Insertleaverequest2")
    public Result<?> insertFeedbackAll2(@RequestParam("stuid") String stuid,// 学生id
                                       @RequestParam("reson") String reson,// 请假原因
                                       @RequestParam("starttime") String starttime,// 开始时间
                                       @RequestParam("overtime") String overtime,// 结束时间
                                       @RequestParam("newFilePath") String newFilePath,// 照片路径
                                       @RequestParam("dlist") String dlist
    ){
        if (reson.isEmpty()||starttime.isEmpty()||overtime.isEmpty()){
            return Result.error("请检查再上传");
        } else {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            Date beginTimeDate = new Date();
            Date endTimeDate = new Date();
            int day = 0;
            String beginTime = starttime;
            String endTime = overtime;

            try {
                beginTimeDate = simpleDateFormat.parse(beginTime);
                endTimeDate = simpleDateFormat.parse(endTime);
                long begin = beginTimeDate.getTime();
                long end = endTimeDate.getTime();
                day = (int) ((end - begin) / (1000 * 60 * 60 * 24));//两个时间天数差
                int hour = (int) ((end - begin) / (1000 * 60 * 60));//两个时间小时差
                int minute = (int) ((end - begin) / (1000 * 60));//两个时间分钟差
            } catch (ParseException e) {
                e.printStackTrace();
            }
            if (day>7){
                return Result.error("大于七天");
            }else {
                try{
                    String[] strs=dlist.split(",");
                    for(int i=0,len=strs.length;i<len;i++){
//                        System.out.println(strs[i].toString());
                        int id = Integer.parseInt(strs[i]);
                        leavereQuestsService.Deletebyid(id);
                    }
                    leavereQuestsService.insertrequest(stuid, starttime, overtime, reson, newFilePath); // 将数据存入数据库
                } catch (Exception e) {
                    e.printStackTrace();
                    return Result.error("提交失败");
                }
                return Result.success("上传成功");
            }


        }

    }

    @ApiOperation("更新请假信息")
    @PostMapping("wx/updateleavequest")
    public List<Map<String,Object>> updatequest(@RequestParam("stuid") String stuid,
                            @RequestParam("starttime") String start,
                            @RequestParam("overtime") String end){
        return leavereQuestsService.startandendbystuid(stuid,start,end);
    }

    /**
     * 数据存储请求（添加请假申请
     * @param stuid 学生id
     * @param reson 请假原因
     * @param starttime 开始时间
     * @param overtime 结束时间
     * @param newFilePath 文件上传后的地址
     * @return
     */
    @ApiOperation("数据存储请求（添加请假申请")
    @PostMapping("wx/Insertleaverequest")
    public Result<?> insertFeedbackAll(@RequestParam("stuid") String stuid,// 学生id
                                       @RequestParam("reson") String reson,// 请假原因
                                       @RequestParam("starttime") String starttime,// 开始时间
                                       @RequestParam("overtime") String overtime,// 结束时间
                                       @RequestParam("newFilePath") String newFilePath
    ){
        if (reson.isEmpty()||starttime.isEmpty()||overtime.isEmpty()){
            return Result.error("请检查再上传");
        } else {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            Date beginTimeDate = new Date();
            Date endTimeDate = new Date();
            int day = 0;
            String beginTime = starttime;
            String endTime = overtime;

            try {
                beginTimeDate = simpleDateFormat.parse(beginTime);
                endTimeDate = simpleDateFormat.parse(endTime);
                long begin = beginTimeDate.getTime();
                long end = endTimeDate.getTime();
                day = (int) ((end - begin) / (1000 * 60 * 60 * 24));//两个时间天数差
                int hour = (int) ((end - begin) / (1000 * 60 * 60));//两个时间小时差
                int minute = (int) ((end - begin) / (1000 * 60));//两个时间分钟差
            } catch (ParseException e) {
                e.printStackTrace();
            }
            if (day>7){
                return Result.error("大于七天");
            }else {
                try{
                    leavereQuestsService.insertrequest(stuid, starttime, overtime, reson, newFilePath); // 将数据存入数据库
                } catch (Exception e) {
                    e.printStackTrace();
                    return Result.error("提交失败");
                }
                return Result.success("上传成功");
            }


        }

    }


}
